﻿using System;
using System.IO;
using UnityEngine;

namespace XUtliPoolLib
{
	// Token: 0x020001B8 RID: 440
	public sealed class XGrid
	{
		// Token: 0x060009E7 RID: 2535 RVA: 0x00033D94 File Offset: 0x00031F94
		public bool LoadFile(string FileName)
		{
			XBinaryReader xbinaryReader = XSingleton<XResourceLoaderMgr>.singleton.ReadBinary(FileName, ".bytes", false, true);
			try
			{
				this.Height = xbinaryReader.ReadInt32();  //地图高
				this.Width = xbinaryReader.ReadInt32();  //地图宽
				this.XMin = xbinaryReader.ReadSingle();  //x最小坐标
				this.ZMin = xbinaryReader.ReadSingle();  //z最小坐标
				this.XMax = xbinaryReader.ReadSingle();  //x最大坐标
				this.ZMax = xbinaryReader.ReadSingle();  //z最大坐标
				this.Side = xbinaryReader.ReadSingle();
				int num = xbinaryReader.ReadInt32();    //索引、数据 数量
				this.IdxData = new int[num];
				for (int i = 0; i < num; i++)
				{
					this.IdxData[i] = xbinaryReader.ReadInt32();
				}
				this.ValueData = new float[num];
				for (int j = 0; j < num; j++)
				{
					short value = xbinaryReader.ReadInt16();
					if (value < 0)
					{
						this.ValueData[j] = -100f;
						if (j + 1 < num)
						{
							if (value == short.MinValue)
							{
								value = 0;
							}
							else
							{
								value = (short)-value;
							}
							this.ValueData[j + 1] = (float)value;
							j++;
						}
					}
					else
					{
						this.ValueData[j] = (float)value;
					}
				}
			}
			catch (EndOfStreamException ex)
			{
				XSingleton<XDebug>.singleton.AddErrorLog("read file ", FileName, " not complete :", ex.Message, null, null);
				return false;
			}
			finally
			{
				XSingleton<XResourceLoaderMgr>.singleton.ClearBinary(xbinaryReader, false);
			}
			return true;
		}

		// Token: 0x060009E8 RID: 2536 RVA: 0x00033F4C File Offset: 0x0003214C
		public bool TryGetHeight(Vector3 pos, out float y)
		{
			y = 0f;
			int num = (int)((pos.z - this.ZMin) / this.Side);
			int num2 = (int)((pos.x - this.XMin) / this.Side);
			int key = num * this.Width + num2;
			int index = this.GetIndex(key);
			bool result;
			if (index >= 0 && index < this.ValueData.Length)
			{
				y = this.ValueData[index] / 100f;
				result = true;
			}
			else
			{
				XSingleton<XDebug>.singleton.AddWarningLog("zcg TryGetHeight Fail............");
				result = false;
			}
			return result;
		}

		// Token: 0x060009E9 RID: 2537 RVA: 0x00033FD8 File Offset: 0x000321D8
		public float GetHeight(Vector3 pos)
		{
			int num = (int)((pos.z - this.ZMin) / this.Side);
			int num2 = (int)((pos.x - this.XMin) / this.Side);
			int key = num * this.Width + num2;
			int index = this.GetIndex(key);
			float result;
			if (index < 0 || index >= this.ValueData.Length)
			{
				XSingleton<XDebug>.singleton.AddErrorLog2("error grid index:{0} pos:{1}", new object[]
				{
					index,
					pos
				});
				result = -1f;
			}
			else
			{
				result = this.ValueData[index] / 100f;
			}
			return result;
		}

		// Token: 0x060009EA RID: 2538 RVA: 0x00034088 File Offset: 0x00032288
		private int GetIndex(int key)
		{
			int i = 0;
			int topIndex = this.IdxData.Length - 1;
			while (i <= topIndex)
			{
				int halfIndex = i + topIndex >> 1;
				int value = this.IdxData[halfIndex];
				if (key == value)
				{
					return halfIndex;
				}
				if (key < value)
				{
					topIndex = halfIndex - 1;
				}
				else if (key > value)
				{
					i = halfIndex + 1;
				}
			}
			//XSingleton<XDebug>.singleton.AddWarningLog("zcg GetIndex Fail............");
			return topIndex;
		}

		// Token: 0x060009EB RID: 2539 RVA: 0x00003284 File Offset: 0x00001484
		public void Update()
		{
		}

		// Token: 0x040004AD RID: 1197
		public int Width;

		// Token: 0x040004AE RID: 1198
		public int Height;

		// Token: 0x040004AF RID: 1199
		public float XMin;

		// Token: 0x040004B0 RID: 1200
		public float ZMin;

		// Token: 0x040004B1 RID: 1201
		public float XMax;

		// Token: 0x040004B2 RID: 1202
		public float ZMax;

		// Token: 0x040004B3 RID: 1203
		public float Side;

		// Token: 0x040004B4 RID: 1204
		public int[] IdxData = null;   //高度图索引数据

		// Token: 0x040004B5 RID: 1205
		public float[] ValueData = null;   //高度图数据
	}
}
